import {Component, ViewChild} from '@angular/core';
import {App, IonicPage, Nav, NavController, NavParams, Platform, Tabs} from "ionic-angular";

import {BackButtonService} from "../../services/common/back-button.service";
import {AboutDetailPage} from "../about-detail/about-detail";
import {JPushService} from "../../services/native/j-push.service";

interface PageInterface {
    name: string;
    index?: number;
}

@IonicPage()
@Component({
    selector: 'home_tabs',
    templateUrl: 'tabs.html'
})
export class TabsPage {

    @ViewChild('rootTabs') tabRef: Tabs;

    tab1Root = 'HomePage';
    tab2Root = 'AboutPage';
    tab3Root = 'ContactPage';
    mySelectedIndex: number;

    constructor(public appCtrl: App,public navCtrl: NavController,public navParams: NavParams, private nav: Nav,public backButtonService: BackButtonService,
                private platform: Platform,jPushService:JPushService) {
        this.mySelectedIndex = navParams.data.tabIndex || 0;

        platform.ready().then(() => {
            this.backButtonService.registerBackButtonAction(this.tabRef);

            //todo app后台驻留时，ios12都触发，Android只触发1,app退出时，ios能收到，点击后只触发两次1
            jPushService.openNotification(appCtrl,navCtrl);
        });

    }

    public openPage(page: PageInterface) {
        let params = {};

        // the nav component was found using @ViewChild(Nav)
        // setRoot on the nav to remove previous pages and only have this page
        // we wouldn't want the back button to show in this scenario
        if (page.index) {
            params = {tabIndex: page.index};
        }

        // If we are already on tabs just change the selected tab
        // don't setRoot again, this maintains the history stack of the
        // tabs even if changing them from the menu
        if (this.nav.getActiveChildNavs().length && page.index != undefined) {
            this.nav.getActiveChildNavs()[0].select(page.index);
            // Set the root of the nav with params if it's a tab index
        } else {
            this.nav.setRoot(page.name, params).catch((err: any) => {
                console.log(`Didn't set nav root: ${err}`);
            });
        }
    }
}
